#!/bin/bash

: << 'COMMENT'
编写时间: 2025-03-19
修改人: 吴建波
编写功能: 加解密服务启动
         1 加解密压缩包解压缩
         2 将压缩包进行删除，并备份
         3 文件启动
COMMENT


# 服务名称
SERVICE_NAME="TC_EnCodeServer"

# 检查服务是否正在运行
PROCESS_INFO=$(ps -ef | grep -v grep | grep "$SERVICE_NAME")

if [ -n "$PROCESS_INFO" ]; then
    echo "服务 $SERVICE_NAME 正在运行。"
    echo "进程信息："
    echo "$PROCESS_INFO"
else
    echo "服务 $SERVICE_NAME 未运行。"
fi


exit 1

# 配置文件
  #name_file_tar="TC_EnCodeSrv-1.0.2.1_COS-7_x86_64_GCC4.8.5.tar.gz" #压缩包名称
  name_file_tar="TC_EnCodeSrv-1.0.2.1_EULER-22.03_x86_64_GCC10.3.1.tar.gz" #压缩包名称
  #TC_EnCodeSrv-1.0.2.1_EULER-22.03_x86_64_GCC10.3.1.tar.gz
  #name_file="TC_EnCodeSrv-1.0.2.1_COS-7_x86_64_GCC4.8.5" #压缩包解密后文件名称
  #name_file="TC_EnCodeSrv-1.0.2.1_EULER-22.03_x86_64_GCC10.3.1" #压缩包解密后文件名称
  name_root="0703-TC_EnCode_Srv" #加解密文件跟目录文件夹

# 路径信息获取
  path_script=$(pwd)
  path_home="$path_script/../$name_root" #加解密文件根目录

  #echo “$path_script,$path_home,$path_start”
# 压缩包解压缩
  path_name_tar_x="$path_home/$name_file_tar" #压缩包路径
  #path_name_tar_c="$path_home/name_file" #解压后路径
  # 压缩包路径
  archive=$path_name_tar_x
  # 判断压缩包是否存在
  if [ -f "$archive" ]; then
      cd $path_home
      echo "--------$(pwd)"
      echo "压缩包 $archive 存在，开始解压..."
      tar -zxvf "$path_name_tar_x"
      # 解压缩
      #files=$(tar -zxvf "$path_name_tar_x" | head -n 1 | awk -F '/' '{print $1}')
      # 解压并获取解压后的根目录名称
      cd
      files=$(tar -zxvf "$path_name_tar_x" | head -n 1 | awk -F '/' '{print $1}')
      # 检查是否成功获取名称
      if [ -n "$files" ]; then
          echo "解压后的根目录名称是: $files"
      else
          echo "未能获取解压后的根目录名称！"
      fi
      #files=$(tar -zxvf $path_name_tar_x)
      #sleep 3
      #exit 1
      name_file=$files
      path_start="$path_home/$name_file/bin/start_server.sh" #服务启动文件
      path_starts="$path_home/$name_file/bin" #服务启动文件汇总
      chmod -R +x $path_starts
      echo "解压完成！启动文件路径是：$path_start"
      echo "$path_home"
  else
      echo "压缩包 $archive 不存在，请检查路径。"
      exit 1
  fi


  # 备份压缩包
  backup_package() {
      local package_path=$1
      if [ -f "$package_path" ]; then
          # 生成备份文件名（带时间戳）
          backup_file="$package_path-$(date +"%Y%m%d.%H%M.%S")"
          # 备份文件
          cp -rf "$package_path" "$backup_file"
          echo "备份成功！备份文件路径：$backup_file"
      else
          echo "压缩包 $package_path 不存在，无法备份。"
          exit 1
      fi
  }
  # 删除压缩包
  delete_package() {
      local package_path=$1
      if [ -f "$package_path" ]; then
          # 删除文件
          rm -rf "$package_path"
          echo "删除成功！压缩包 $package_path 已删除。"
      else
          echo "压缩包 $package_path 不存在，无需删除。"
          exit 1
      fi
  }
  package_path=$path_name_tar_x
  if [ -z "$package_path" ]; then
      echo "错误：请传入压缩包路径作为参数。"
      exit 1
  fi
  # 备份压缩包
  backup_package "$package_path"
  # 删除压缩包
  #delete_package "$package_path"


#服务启动
  # 定义启动文件路径
  #start_script="/root/autodeploy/../0703-TC_EnCode_Srv/TC_EnCodeSrv-1.0.2.1_COS-7_x86_64_GCC4.8.5/bin/start_server.sh"
  start_script=$path_start


  # 定义服务名称（用于查找进程）
  service_name="TC_En"
  # 判断启动文件是否存在
  if [ ! -f "$start_script" ]; then
      echo "启动文件 $start_script 不存在，流程终止。"
      exit 1
  fi
  chmod +x $start_script
  echo "--------$start_script"
  # 判断服务是否启动
  pid=$(pgrep -f "$service_name")
  echo "11----$pid,$service_name"
  if [ -z "$pid" ]; then
      echo "服务未启动，正在启动服务..."
      # 启动服务
      #"$start_script"
      cd $path_starts #进入文件夹的bin执行路径
      #sh -x "$start_script &"
      #exit 1
      echo "----$(pwd)"
      #exit 1
      "sh -x start_server.sh &"
      exit 1
      sleep 5
      if [ $? -eq 0 ]; then
          echo "服务启动成功！"
      else
          echo "服务启动失败！"
      fi
  else
      echo "服务已启动，进程ID为 $pid。"
      # 提示是否杀死进程后重启
      read -p "是否杀死进程并重启服务？(y/n): " choice
      if [ "$choice" = "y" ] || [ "$choice" = "Y" ]; then
          echo "正在杀死进程 $pid..."
          cd $path_starts #进入文件夹的bin执行路径
          echo "----$(pwd)"
          #echo "----$(ls)"
          #"./stop_server.sh &"
          #""
          sleep 1
          #kill -9 "$pid"
          if [ $? -eq 0 ]; then
              echo "进程已杀死，正在启动服务..."
              sleep 5
              "./start_server.sh &"
              if [ $? -eq 0 ]; then
                  echo "服务启动成功！"
              else
                  echo "服务启动失败！"
              fi
          else
              echo "杀死进程失败！"
          fi
      else
          echo "未执行任何操作，流程终止。"
      fi
  fi

